﻿Imports System
Imports System.Collections.Generic
Imports System.Globalization

Namespace System.Workflow.ComponentModel
    Public NotInheritable Class ActivityExecutionContext
        Implements IServiceProvider, IDisposable
        ' Methods
        Friend Sub New(ByVal activity As Activity)
            Me.currentActivity = activity
        End Sub

        Friend Sub New(ByVal activity As Activity, ByVal allowSignalsOnCurrentActivity As Boolean)
            Me.New(activity)
            Me.allowSignalsOnCurrentActivity = allowSignalsOnCurrentActivity
        End Sub

        Friend Function AcquireLocks(ByVal locksAcquiredCallback As IActivityEventListener([Of] EventArgs)) As Boolean
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.Activity.SetValue(ActivityExecutionContext.LockAcquiredCallbackProperty, New ActivityExecutorDelegateInfo([Of] EventArgs)(True, locksAcquiredCallback, Me.Activity.ContextActivity))
            Return Me.AcquireLocks(Me.Activity)
        End Function

        Private Function AcquireLocks(ByVal activity As Activity) As Boolean
            Dim allSynchronizationHandles As ICollection([Of] String) = Me.GetAllSynchronizationHandles(activity)
            If ((Not allSynchronizationHandles Is Nothing) AndAlso (allSynchronizationHandles.Count <> 0)) Then
                Dim activity2 As Activity = activity.Parent
                Do While (Not activity2 Is Nothing)
                    If (activity2.SupportsSynchronization OrElse (activity2.Parent Is Nothing)) Then
                        Dim dictionary As Dictionary([Of] String, GrantedLock) = DirectCast(activity2.GetValue(ActivityExecutionContext.GrantedLocksProperty), Dictionary([Of] String, GrantedLock))
                        If (dictionary Is Nothing) Then
                            dictionary = New Dictionary([Of] String, GrantedLock)
                            activity2.SetValue(ActivityExecutionContext.GrantedLocksProperty, dictionary)
                        End If
                        Dim str As String
                        For Each str In allSynchronizationHandles
                            Dim flag As Boolean = True
                            If Not dictionary.ContainsKey(str) Then
                                dictionary.Item(str) = New GrantedLock(activity)
                            ElseIf (Not dictionary.Item(str).Holder Is activity) Then
                                dictionary.Item(str).WaitList.Add(activity)
                                flag = False
                            End If
                            If Not flag Then
                                Return False
                            End If
                        Next
                    End If
                    Dim is3 As ICollection([Of] String) = DirectCast(activity2.GetValue(Activity.SynchronizationHandlesProperty), ICollection([Of] String))
                    If ((Not is3 Is Nothing) AndAlso (is3.Count <> 0)) Then
                        Exit Do
                    End If
                    activity2 = activity2.Parent
                Loop
            End If
            Return True
        End Function

        Public Sub CancelActivity(ByVal activity As Activity)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If (Not Me.allowSignalsOnCurrentActivity AndAlso ((Me.currentActivity.WorkflowCoreRuntime.CurrentActivity.ExecutionStatus = ActivityExecutionStatus.Initialized) OrElse (Me.currentActivity.WorkflowCoreRuntime.CurrentActivity.ExecutionStatus = ActivityExecutionStatus.Closed))) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidStateToExecuteChild"))
            End If
            If Not Me.IsValidChild(activity, False) Then
                Throw New ArgumentException(SR.GetString("AEC_InvalidActivity"), "activity")
            End If
            If (activity.ExecutionStatus <> ActivityExecutionStatus.Executing) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidCancelingState"))
            End If
            Try 
                activity.SetStatus(ActivityExecutionStatus.Canceling, False)
            Finally
                Me.currentActivity.WorkflowCoreRuntime.ScheduleItem(New ActivityExecutorOperation(activity, ActivityOperationType.Cancel, Me.ContextId), ActivityExecutionContext.IsInAtomicTransaction(activity), False, False)
            End Try
        End Sub

        Friend Sub CheckpointInstanceState()
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.currentActivity.WorkflowCoreRuntime.CheckpointInstanceState(Me.currentActivity)
        End Sub

        Public Sub CloseActivity()
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Select Case Me.currentActivity.ExecutionStatus
                Case ActivityExecutionStatus.Executing
                    Me.currentActivity.MarkCompleted
                    Return
                Case ActivityExecutionStatus.Canceling
                    Me.currentActivity.MarkCanceled
                    Return
                Case ActivityExecutionStatus.Closed
                    Return
                Case ActivityExecutionStatus.Compensating
                    Me.currentActivity.MarkCompensated
                    Return
                Case ActivityExecutionStatus.Faulting
                    Me.currentActivity.MarkFaulted
                    Return
            End Select
            Throw New InvalidOperationException(SR.GetString("Error_InvalidClosingState"))
        End Sub

        Friend Sub CompensateActivity(ByVal activity As Activity)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If Not Me.IsValidNestedChild(activity) Then
                Throw New ArgumentException(SR.GetString("AEC_InvalidNestedActivity"), "activity")
            End If
            If (activity.ExecutionStatus <> ActivityExecutionStatus.Closed) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidCompensatingState"))
            End If
            Try 
                activity.SetStatus(ActivityExecutionStatus.Compensating, False)
            Finally
                Me.currentActivity.WorkflowCoreRuntime.ScheduleItem(New ActivityExecutorOperation(activity, ActivityOperationType.Compensate, Me.ContextId), ActivityExecutionContext.IsInAtomicTransaction(activity), False, False)
            End Try
        End Sub

        Friend Sub DisposeCheckpointState()
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.currentActivity.WorkflowCoreRuntime.DisposeCheckpointState
        End Sub

        Private Shared Sub EnforceExceptionSemantics(ByVal d As DependencyObject, ByVal value As Object)
            If Not TypeOf d Is Activity Then
                Throw New ArgumentException(SR.GetString(CultureInfo.CurrentCulture, "Error_DOIsNotAnActivity"))
            End If
            If (Not value Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, "Error_PropertyCanBeOnlyCleared"))
            End If
            d.SetValueCommon(ActivityExecutionContext.CurrentExceptionProperty, Nothing, ActivityExecutionContext.CurrentExceptionProperty.DefaultMetadata, False)
        End Sub

        Public Sub ExecuteActivity(ByVal activity As Activity)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If (Not Me.allowSignalsOnCurrentActivity AndAlso ((Me.currentActivity.WorkflowCoreRuntime.CurrentActivity.ExecutionStatus = ActivityExecutionStatus.Initialized) OrElse (Me.currentActivity.WorkflowCoreRuntime.CurrentActivity.ExecutionStatus = ActivityExecutionStatus.Closed))) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidStateToExecuteChild"))
            End If
            If Not Me.IsValidChild(activity, False) Then
                Throw New ArgumentException(SR.GetString("AEC_InvalidActivity"), "activity")
            End If
            If (activity.ExecutionStatus <> ActivityExecutionStatus.Initialized) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidExecutionState"))
            End If
            Try 
                activity.SetStatus(ActivityExecutionStatus.Executing, False)
            Finally
                Me.currentActivity.WorkflowCoreRuntime.ScheduleItem(New ActivityExecutorOperation(activity, ActivityOperationType.Execute, Me.ContextId), ActivityExecutionContext.IsInAtomicTransaction(activity), False, False)
            End Try
        End Sub

        Friend Sub FaultActivity(ByVal e As Exception)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (Me.currentActivity.ExecutionStatus = ActivityExecutionStatus.Closed) Then
                If (Me.currentActivity.Parent Is Nothing) Then
                    Me.currentActivity.WorkflowCoreRuntime.TerminateInstance(e)
                Else
                    Me.currentActivity.WorkflowCoreRuntime.RaiseException(e, Me.currentActivity.Parent, String.Empty)
                End If
            Else
                Try 
                    Me.currentActivity.SetValueCommon(ActivityExecutionContext.CurrentExceptionProperty, e, ActivityExecutionContext.CurrentExceptionProperty.DefaultMetadata, False)
                    Me.currentActivity.SetStatus(ActivityExecutionStatus.Faulting, False)
                Finally
                    Me.currentActivity.WorkflowCoreRuntime.ScheduleItem(New ActivityExecutorOperation(Me.currentActivity, ActivityOperationType.HandleFault, Me.ContextId, e), ActivityExecutionContext.IsInAtomicTransaction(Me.currentActivity), False, False)
                End Try
            End If
        End Sub

        Private Function GetAllSynchronizationHandles(ByVal activity As Activity) As ICollection([Of] String)
            Dim handler As WalkerEventHandler = Nothing
            Dim is2 As ICollection([Of] String) = DirectCast(activity.GetValue(Activity.SynchronizationHandlesProperty), ICollection([Of] String))
            If ((is2 Is Nothing) OrElse (is2.Count = 0)) Then
                Return is2
            End If
            Dim handles As New List([Of] String)(is2)
            If TypeOf activity Is CompositeActivity Then
                Dim walker As New Walker
                If (handler Is Nothing) Then
                    handler = Function (ByVal w As Walker, ByVal e As WalkerEventArgs) 
                        If (Not e.CurrentActivity Is activity) Then
                            Dim collection As ICollection([Of] String) = DirectCast(e.CurrentActivity.GetValue(Activity.SynchronizationHandlesProperty), ICollection([Of] String))
                            If (Not collection Is Nothing) Then
                                [handles].AddRange(collection)
                            End If
                        End If
                    End Function
                End If
                AddHandler walker.FoundActivity, handler
                walker.Walk(activity)
            End If
            [handles].Sort
            Dim i As Integer
            For i = 1 To [handles].Count - 1
                If ([handles].Item(i) = [handles].Item((i - 1))) Then
                    [handles].RemoveAt(--i)
                End If
            Next i
            [handles].TrimExcess
            Return [handles]
        End Function

        Public Function GetService([Of] T)() As T
            Return DirectCast(Me.GetService(GetType(T)), T)
        End Function

        Public Function GetService(ByVal serviceType As Type) As Object
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (serviceType Is GetType(IStartWorkflow)) Then
                If (Me.startWorkflowService Is Nothing) Then
                    Me.startWorkflowService = New StartWorkflow(Me)
                End If
                Return Me.startWorkflowService
            End If
            If ((Not ActivityExecutionContext.schedulerServiceType Is Nothing) AndAlso ActivityExecutionContext.schedulerServiceType.IsAssignableFrom(serviceType)) Then
                Return Nothing
            End If
            If ((Not ActivityExecutionContext.persistenceServiceType Is Nothing) AndAlso ActivityExecutionContext.persistenceServiceType.IsAssignableFrom(serviceType)) Then
                Return Nothing
            End If
            If ((Not ActivityExecutionContext.trackingServiceType Is Nothing) AndAlso ActivityExecutionContext.trackingServiceType.IsAssignableFrom(serviceType)) Then
                Return Nothing
            End If
            If ((Not ActivityExecutionContext.transactionServiceType Is Nothing) AndAlso ActivityExecutionContext.transactionServiceType.IsAssignableFrom(serviceType)) Then
                Return Nothing
            End If
            If ((Not ActivityExecutionContext.loaderServiceType Is Nothing) AndAlso ActivityExecutionContext.loaderServiceType.IsAssignableFrom(serviceType)) Then
                Return Nothing
            End If
            Return Me.currentActivity.WorkflowCoreRuntime.GetService(Me.currentActivity, serviceType)
        End Function

        Friend Sub InitializeActivity(ByVal activity As Activity)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If Not Me.IsValidChild(activity, False) Then
                Throw New ArgumentException(SR.GetString("AEC_InvalidActivity"), "activity")
            End If
            If (activity.ExecutionStatus <> ActivityExecutionStatus.Initialized) Then
                Throw New InvalidOperationException(SR.GetString("Error_InvalidInitializingState"))
            End If
            [Using] context As ActivityExecutionContext = New ActivityExecutionContext(activity)
                [Using] Me.currentActivity.WorkflowCoreRuntime.SetCurrentActivity(activity)
                    activity.Initialize(context)
                End [Using]
            End [Using]
        End Sub

        Friend Sub Invoke([Of] T As EventArgs)(ByVal handler As EventHandler([Of] T), ByVal e As T)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.currentActivity.Invoke([Of] T)(handler, e)
        End Sub

        Friend Shared Function IsInAtomicTransaction(ByVal activity As Activity) As Boolean
            Do While (Not activity Is Nothing)
                If (activity Is activity.WorkflowCoreRuntime.CurrentAtomicActivity) Then
                    Return True
                End If
                activity = activity.Parent
            Loop
            Return False
        End Function

        Friend Function IsValidChild(ByVal activity As Activity, ByVal allowContextVariance As Boolean) As Boolean
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (((Not activity Is Me.currentActivity.WorkflowCoreRuntime.CurrentActivity) OrElse Not Me.allowSignalsOnCurrentActivity) AndAlso ((Not activity.Enabled OrElse (Not activity.Parent Is Me.currentActivity.WorkflowCoreRuntime.CurrentActivity)) OrElse (Not allowContextVariance AndAlso Not activity.Equals(Me.Activity.GetActivityByName(activity.QualifiedName, True))))) Then
                Return False
            End If
            Return True
        End Function

        Friend Function IsValidNestedChild(ByVal activity As Activity) As Boolean
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (activity Is Me.currentActivity) Then
                Return True
            End If
            Dim parent As Activity = activity
            Do While (((Not parent Is Nothing) AndAlso parent.Enabled) AndAlso (Not parent.Parent Is Me.currentActivity.ContextActivity))
                parent = parent.Parent
            Loop
            Return ((Not parent Is Nothing) AndAlso parent.Enabled)
        End Function

        Friend Sub ReleaseLocks(ByVal transactional As Boolean)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.Activity.RemoveProperty(ActivityExecutionContext.LockAcquiredCallbackProperty)
            Dim allSynchronizationHandles As ICollection([Of] String) = Me.GetAllSynchronizationHandles(Me.Activity)
            If ((Not allSynchronizationHandles Is Nothing) AndAlso (allSynchronizationHandles.Count <> 0)) Then
                Dim list As New List([Of] Activity)
                Dim activity As Activity = Me.Activity.Parent
                Do While (Not activity Is Nothing)
                    If (activity.SupportsSynchronization OrElse (activity.Parent Is Nothing)) Then
                        Dim dictionary As Dictionary([Of] String, GrantedLock) = DirectCast(activity.GetValue(ActivityExecutionContext.GrantedLocksProperty), Dictionary([Of] String, GrantedLock))
                        If transactional Then
                            Dim dictionary2 As New Dictionary([Of] String, GrantedLock)
                            If (Not dictionary Is Nothing) Then
                                Dim pair As KeyValuePair([Of] String, GrantedLock)
                                For Each pair In dictionary
                                    dictionary2.Add(pair.Key, DirectCast(pair.Value.Clone, GrantedLock))
                                Next
                            End If
                            activity.SetValue(ActivityExecutionContext.CachedGrantedLocksProperty, dictionary2)
                        End If
                        If (Not dictionary Is Nothing) Then
                            Dim str As String
                            For Each str In allSynchronizationHandles
                                If dictionary.ContainsKey(str) Then
                                    If (dictionary.Item(str).WaitList.Count = 0) Then
                                        dictionary.Remove(str)
                                    Else
                                        If (Not dictionary.Item(str).Holder Is Me.Activity) Then
                                            dictionary.Item(str).WaitList.Remove(Me.Activity)
                                            [Continue] For
                                        End If
                                        Dim item As Activity = dictionary.Item(str).WaitList.Item(0)
                                        dictionary.Item(str).WaitList.RemoveAt(0)
                                        dictionary.Item(str).Holder = item
                                        If Not list.Contains(item) Then
                                            list.Add(item)
                                        End If
                                    End If
                                End If
                            Next
                            If (dictionary.Count = 0) Then
                                activity.RemoveProperty(ActivityExecutionContext.GrantedLocksProperty)
                            End If
                        End If
                    End If
                    Dim is3 As ICollection([Of] String) = DirectCast(activity.GetValue(Activity.SynchronizationHandlesProperty), ICollection([Of] String))
                    If ((Not is3 Is Nothing) AndAlso (is3.Count <> 0)) Then
                        Exit Do
                    End If
                    activity = activity.Parent
                Loop
                Dim activity3 As Activity
                For Each activity3 In list
                    If Me.AcquireLocks(activity3) Then
                        DirectCast(activity3.GetValue(ActivityExecutionContext.LockAcquiredCallbackProperty), ActivityExecutorDelegateInfo([Of] EventArgs)).InvokeDelegate(Me.Activity.ContextActivity, EventArgs.Empty, False, transactional)
                    End If
                Next
            End If
        End Sub

        Friend Sub RequestRevertToCheckpointState(ByVal handler As EventHandler([Of] EventArgs), ByVal data As EventArgs, ByVal suspendOnRevert As Boolean, ByVal suspendOnRevertInfo As String)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.currentActivity.WorkflowCoreRuntime.RequestRevertToCheckpointState(Me.currentActivity, handler, data, suspendOnRevert, suspendOnRevertInfo)
        End Sub

        Friend Sub SuspendWorkflowInstance(ByVal suspendDescription As String)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            Me.currentActivity.WorkflowCoreRuntime.SuspendInstance(suspendDescription)
        End Sub

        Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
            If (Not Me.currentActivity Is Nothing) Then
                If (Not Me.contextManager Is Nothing) Then
                    Me.contextManager.Dispose
                    Me.contextManager = Nothing
                End If
                Me.currentActivity = Nothing
            End If
        End Sub

        Friend Sub TerminateWorkflowInstance(ByVal e As Exception)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (e Is Nothing) Then
                Throw New ArgumentNullException("e")
            End If
            Me.currentActivity.WorkflowCoreRuntime.TerminateInstance(e)
        End Sub

        Public Sub TrackData(ByVal userData As Object)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (userData Is Nothing) Then
                Throw New ArgumentNullException("userData")
            End If
            Me.currentActivity.WorkflowCoreRuntime.Track(Nothing, userData)
        End Sub

        Public Sub TrackData(ByVal userDataKey As String, ByVal userData As Object)
            If (Me.currentActivity Is Nothing) Then
                Throw New ObjectDisposedException("ActivityExecutionContext")
            End If
            If (userData Is Nothing) Then
                Throw New ArgumentNullException("userData")
            End If
            Me.currentActivity.WorkflowCoreRuntime.Track(userDataKey, userData)
        End Sub


        ' Properties
        Public ReadOnly Property Activity As Activity
            Get
                If (Me.currentActivity Is Nothing) Then
                    Throw New ObjectDisposedException("ActivityExecutionContext")
                End If
                Return Me.currentActivity
            End Get
        End Property

        Public ReadOnly Property ContextGuid As Guid
            Get
                If (Me.currentActivity Is Nothing) Then
                    Throw New ObjectDisposedException("ActivityExecutionContext")
                End If
                Return Me.currentActivity.ContextActivity.ContextGuid
            End Get
        End Property

        Friend ReadOnly Property ContextId As Integer
            Get
                If (Me.currentActivity Is Nothing) Then
                    Throw New ObjectDisposedException("ActivityExecutionContext")
                End If
                Return Me.currentActivity.ContextActivity.ContextId
            End Get
        End Property

        Public ReadOnly Property ExecutionContextManager As ActivityExecutionContextManager
            Get
                If (Me.currentActivity Is Nothing) Then
                    Throw New ObjectDisposedException("ActivityExecutionContext")
                End If
                If (Me.contextManager Is Nothing) Then
                    Me.contextManager = New ActivityExecutionContextManager(Me)
                End If
                Return Me.contextManager
            End Get
        End Property

        Friend ReadOnly Property WorkflowCoreRuntime As IWorkflowCoreRuntime
            Get
                If (Me.currentActivity Is Nothing) Then
                    Throw New ObjectDisposedException("ActivityExecutionContext")
                End If
                Return Me.GetService([Of] IWorkflowCoreRuntime)
            End Get
        End Property


        ' Fields
        Private allowSignalsOnCurrentActivity As Boolean
        Friend Shared ReadOnly CachedGrantedLocksProperty As DependencyProperty = DependencyProperty.RegisterAttached("CachedGrantedLocks", GetType(Dictionary([Of] String, GrantedLock)), GetType(ActivityExecutionContext), New PropertyMetadata(DependencyPropertyOptions.NonSerialized))
        Private contextManager As ActivityExecutionContextManager
        Private currentActivity As Activity
        Public Shared ReadOnly CurrentExceptionProperty As DependencyProperty = DependencyProperty.RegisterAttached("CurrentException", GetType(Exception), GetType(ActivityExecutionContext), New PropertyMetadata(Nothing, DependencyPropertyOptions.Default, Nothing, New SetValueOverride(AddressOf ActivityExecutionContext.EnforceExceptionSemantics), True, New Attribute(0  - 1) {}))
        Friend Shared ReadOnly GrantedLocksProperty As DependencyProperty = DependencyProperty.RegisterAttached("GrantedLocks", GetType(Dictionary([Of] String, GrantedLock)), GetType(ActivityExecutionContext))
        Private Shared loaderServiceType As Type = Type.GetType("System.Workflow.Runtime.Hosting.WorkflowLoaderService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL")
        Friend Shared ReadOnly LockAcquiredCallbackProperty As DependencyProperty = DependencyProperty.RegisterAttached("LockAcquiredCallback", GetType(ActivityExecutorDelegateInfo([Of] EventArgs)), GetType(ActivityExecutionContext))
        Private Shared persistenceServiceType As Type = Type.GetType("System.Workflow.Runtime.Hosting.WorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL")
        Private Shared schedulerServiceType As Type = Type.GetType("System.Workflow.Runtime.Hosting.WorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL")
        Private startWorkflowService As IStartWorkflow
        Private Shared trackingServiceType As Type = Type.GetType("System.Workflow.Runtime.Tracking.TrackingService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL")
        Private Shared transactionServiceType As Type = Type.GetType("System.Workflow.Runtime.Hosting.WorkflowCommitWorkBatchService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL")

        ' Nested Types
        Friend NotInheritable Class StartWorkflow
            Implements IStartWorkflow
            ' Methods
            Friend Sub New(ByVal executionContext As ActivityExecutionContext)
                Me.executionContext = executionContext
            End Sub

            Private Function System.Workflow.ComponentModel.IStartWorkflow.StartWorkflow(ByVal workflowType As Type, ByVal namedArgumentValues As Dictionary([Of] String, Object)) As Guid Implements IStartWorkflow.StartWorkflow
                Return Me.executionContext.WorkflowCoreRuntime.StartWorkflow(workflowType, namedArgumentValues)
            End Function


            ' Fields
            Private executionContext As ActivityExecutionContext
        End Class
    End Class
End Namespace

